//
//  ReactNativeSingleViewController.m
//  ModifyMetroDemo
//
//  Created by Marcus on 2020/3/8.
//  Copyright © 2020 Facebook. All rights reserved.
//

#import "RNSingleViewController.h"
#import <React/RCTBridge.h>
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
#import "RNTimeUtils.h"
#import "SharePerenceHelper.h"

@interface RNSingleViewController ()

@end

@implementation RNSingleViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  // Do any additional setup after loading the view.
    
  self.businessInfo = [[SharePerenceHelper instance] getReactNativeBusinessInfo];

  NSString* title = [[NSString alloc] initWithFormat:@"Sync: %@", self.businessInfo.displayName];
  [self.navigationItem setTitle:title ];
  
  UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 20)]; // 数字的顺序是 x， y ，宽度，高度
  label.text=@"Loading...";// 文本内容
  label.font = [UIFont systemFontOfSize:14];// 字体大小
  label.textColor = [UIColor redColor]; // 文本颜色
  [self.view addSubview:label];
  
  [self initReactNativeContext];
}

- (void) initReactNativeContext{
  [[NSNotificationCenter defaultCenter] addObserver:self
                                            selector:@selector(handleBridgeDidLoadJavaScriptNotification:)
                                                name:RCTContentDidAppearNotification
                                              object:nil];
  
  
  NSDictionary *launchOptions = [[NSDictionary alloc] init];
  RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self
                                        launchOptions:launchOptions];
  
  RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
                                                   moduleName:self.businessInfo.mainComponentName
                                            initialProperties:nil];
  rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];
  self.view = rootView;
}

- (void)handleBridgeDidLoadJavaScriptNotification:(NSNotification *)notification
{
   // todo
  [RNTimeUtils.instance endTimeRecord];
  [RNTimeUtils.instance printTimeCost];
}

- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge{
  return  [[NSBundle mainBundle] URLForResource:self.businessInfo.orgBundleFilePath
                                  withExtension:@"bundle"];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
